<?php
/**
  * @usage:
  *	$arr = array(1 => array('name'=>'nana', 'key' => array('name'=>'test', 'sex'=>'male')),'name'=>87);
  *	FormatShow::printr($arr);
  *
  */
class FormatShow
{
	const LOG_FILENAME = null;

	protected $content;
	protected $lineNum;
	protected $starLineNum;
	protected $endLineNum;
	protected $uniqueNum;

	public function __construct()
	{	
	}

	public static function pout() 
	{
        if (self::LOG_FILENAME) 
        {
            ob_start();
        }

        echo "<div align='left'><pre>\n";//begin show
        $i = 0;
        $data = func_get_args();

        foreach ($data as $v) 
        {			
            if ($i > 0) 
            {
                echo "<div style='color:blue'>".str_repeat('-', '50')."</div>\n";//sperate line
            }

            $rand = rand(100000000, 300000000);

            if (is_array($v) || is_object($v))//array and object
				{
					echo self::format($v, $rand);
				} 
				else //use var_dump
				{
					var_dump($v);
				}
				$i++;
			} // end-foreach

			echo "</pre></div>";//begin show
			echo self::getExpandScript();//output js function for expand

			if (self::LOG_FILENAME) 
			{
				self::writeFile();
			}
	} // end function

	//$data is array or object type
	private static function format($data, $uniqueNum) 
	{
		$export_str = print_r($data, true);
		$data       = explode("\n", $export_str);
		$formatshows= array();
		$formatshow_stack = array();

		$class_name = __CLASS__;
		
		foreach($data as $k => $v)
		{
			$formatshow            = new $class_name();
			$formatshow->content   = $v;
			$formatshow->uniqueNum = $uniqueNum;
			$formatshow->lineNum   = $k;

			if (strpos($v, "Object") === strlen($v) - 6 || strpos($v, "Array") === strlen($v) - 5)
			{
				$formatshow->starLineNum = $k;
				array_push($formatshow_stack, $formatshow);
			}
			elseif (trim($v) === ")")
			{
				$obj = array_pop($formatshow_stack);
				$obj->endLineNum = $k;
			}
			array_push($formatshows, $formatshow);
		}

		$html = '';
		foreach($formatshows as $v)
		{
			$html .= $v->buildDivHtml();
		}

		return $html;
	}

	public function buildDivHtml()
	{
		
		if ($this->starLineNum !== NULL)
		{
			$attr =  "startLineNum='$this->starLineNum' endLineNum='$this->endLineNum' onclick='__expand(this)' display='block'";
		}
		else
		{
			$attr = "";
		}

		$div = "<div uniqueNum='$this->uniqueNum' id='formatshow_".$this->uniqueNum."_".$this->lineNum."' ".$attr.">$this->content</div>";

		return $div;
	}

	public static function writeFile()
	{
		$content = ob_get_contents();
		ob_end_clean();
		$status = file_put_contents(self::LOG_FILENAME, $content);

		if ($status === false) 
		{
			echo $content;
			error_log('write file fail');
		}	

		return $status;
	}

	public static function getExpandScript()
	{
			return "<script>
			function __expand(div_ele)
			{
				var uniqueNum    = div_ele.getAttribute('uniqueNum');
				var startLineNum = div_ele.getAttribute('startLineNum') * 1;
				var endLineNum   = div_ele.getAttribute('endLineNum') * 1;
				var display      = div_ele.getAttribute('display') == 'block' ? 'none' : 'block';
				var id_prefix    = 'formatshow_' + uniqueNum + '_';
				var element;
				
				div_ele.setAttribute('display', display);
				div_ele.style.backgroundColor = (display == 'block') ? '' : 'red';

				for (var i = startLineNum + 1; i <= endLineNum; i++)
				{
					id = id_prefix + i;
					element = document.getElementById(id);
					element.style.display = display;

					if (element.getAttribute('display') == 'none')
					{
						i = i + 1 + element.getAttribute('endLineNum') * 1 - element.getAttribute('startLineNum') * 1;
					}
				}
			}
		</script>";	
	}

}//end class

class FS extends FormatShow{}

